
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

//stack to store the world matrix
#include <stack>
using std::stack;

//list of nodes
#include <list>
using std::list;


//matrix class
#include "RTMatrix44.h"

namespace RT
{
	//root
	class RTNode;

	//specific node class
	class RTNodeTranslate;
	class RTNodeSeparator;
	class RTNodeScale;
	class RTNodeRotate;
	class RTNodePrimitive;
	class RTIPrimitive; //necessary to create a RTPrimitive node
	class RTNodeMaterial;
	class RTMaterial; //necessary to create a material node
	class RTNodeLookAt;
	class RTNodeDirectionalLight;
	class RTDirectionalLight; //necessary to create a directional light node
	class RTNodePointLight;
	class RTPointLight; //necessary to create a point light node

	/*scenegraph class organized as a tree. Used to update the scene before every frame. Can create nodes.*/
	class RTTree
	{
	private:
		//singleton instance
		static RTTree*	_instance;

		//poI32er to the root of the scenegraph
		RTNode*			_root;

		//current value of the world matrix in the scenegraph
		RTMatrix44			_value;

		//matrix stack used to push and pop world matrix
		stack<RTMatrix44>	_stack;

		//Storage of node's poI32er
		list<RTNode*> _nodesList;

	public:
		//used to get the unique instance of the object RTTree. Do not use this, use SGTREE instead
		static RTTree& getInstance();

		//close the singleton
		void close();

		//set the value of the current matrix
		void setValue(const RTMatrix44& value);

		//get the value of the current matrix
		RTMatrix44& getValue();

		//put on pile the current world matrix
		void pop();

		//push from pile to the current world matrx
		void push();

		//dirty all the controller to calculate the next iteration
		void dirty();

		//traverse the tree and update all the nodes
		void traversal();

		//set the root of the tree
		void setRoot(RTNode* root);

		//create a translate node and return its poI32er
		RTNodeTranslate* createNodeTranslate();

		//create a separator node and return its poI32er
		RTNodeSeparator* createNodeSeparator();

		//create a scale node and return its poI32er
		RTNodeScale* createNodeScale();

		//create a rotate node and return its poI32er
		RTNodeRotate* createNodeRotate();

		//create a RTPrimitive node and return its poI32er
		RTNodePrimitive* createNodePrimitive(RTIPrimitive*);

		//create a meterial node and return its poI32er
		RTNodeMaterial* createNodeMaterial(RTMaterial*);

		//create a lookat node and return its poI32er
		RTNodeLookAt* createNodeLookAt();

		//create a directional light node and return its poI32er
		RTNodeDirectionalLight* createNodeDirectionalLight(RTDirectionalLight*);

		//create a poI32 light node and return its poI32er
		RTNodePointLight* createNodePointLight(RTPointLight*);

	private:
		//Constructor
		RTTree(void);

		//Destructor
		~RTTree(void);
	};

	//macro used to get access to the singleton
	#define SGTREE RTTree::getInstance()

}